package com.example.admin.jula_xu;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button top_bar_linear_back;
    private TextView top_bar_title;
    private RecyclerView recyclerView_one;
    private RecyclerView.Adapter mAdapter;
    private LinearLayoutManager mLayoutManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        top_bar_linear_back=(Button)findViewById(R.id.btn);
        top_bar_linear_back.setOnClickListener(new CustomOnClickListener());
        top_bar_title=(TextView)this.findViewById(R.id.tv2);
        top_bar_title.setText("RecyclerView使用实例");
        //开始设置RecyclerView
        recyclerView_one=(RecyclerView)this.findViewById(R.id.recycler_view_test_rv);
        //设置固定大小
        recyclerView_one.setHasFixedSize(true);
        //创建线性布局
        mLayoutManager = new LinearLayoutManager(this);
        //垂直方向
        mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        //给RecyclerView设置布局管理器
        recyclerView_one.setLayoutManager(mLayoutManager);
        //创建适配器，并且设置
        mAdapter = new TestRecyclerAdapter(this);





        
        recyclerView_one.setAdapter(mAdapter);
        recyclerView_one.addItemDecoration(new AdvanceDecoration(this,OrientationHelper.VERTICAL));
    }
    class CustomOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            MainActivity.this.finish();
        }
    }

    class AdvanceDecoration extends RecyclerView.ItemDecoration {
        //采用系统内置的风格的分割线
        private final int[] attrs = new int[]{android.R.attr.listDivider};
        private Drawable mDivider;
        private int orientation;

        public AdvanceDecoration(Context context, int orientation) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs);
            mDivider = typedArray.getDrawable(0);
            typedArray.recycle();
            this.orientation = orientation;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            drawHDeraction(c, parent);
//            drawVDeraction(c, parent);
        }

        /**
         * 绘制水平方向的分割线
         *
         * @param c
         * @param parent
         */
        private void drawHDeraction(Canvas c, RecyclerView parent) {
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
                int top = child.getBottom() + layoutParams.bottomMargin;
                int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

//    private static final int[] attrs=new int[]{android.R.attr.listDivider};
//    private Drawable mDivider;


//    class TestDecoration extends RecyclerView.ItemDecoration {
//        public TestDecoration(Context context) {
//            TypedArray typedArray = context.obtainStyledAttributes(attrs);
//            mDivider = typedArray.getDrawable(0);
//        }
//        @Override
//        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//            int top=parent.getPaddingTop();
//            int bottom=parent.getHeight()-parent.getPaddingBottom();
//            int childCount=parent.getChildCount();
//            for(int i=0;i<childCount;i++){
//                View child=parent.getChildAt(i);
//                RecyclerView.LayoutParams layoutParams=(RecyclerView.LayoutParams)child.getLayoutParams();
//                int left=child.getRight()+layoutParams.rightMargin;
//                int right=left+mDivider.getIntrinsicWidth();
//                mDivider.setBounds(left,top,right,bottom);
//                mDivider.draw(c);
//            }
//        }
//
//        @Override
//        public void getItemOffsets(Rect outRect,View view, RecyclerView parent, RecyclerView.State state) {
//            outRect.set(0,0,mDivider.getIntrinsicWidth(),0);
//        }
//    }
}
